package com.jiamao.goplay.ui.photo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gyf.immersionbar.ImmersionBar;
import com.jiamao.goplay.BR;
import com.jiamao.goplay.R;
import com.jiamao.goplay.adapter.UserWanToGoAdapter;
import com.jiamao.goplay.app.AppViewModelFactory;
import com.jiamao.goplay.base.BaseMVVMFragment;
import com.jiamao.goplay.databinding.PhotoUploadLayoutBinding;
import com.jiamao.goplay.databinding.PhotoViewLayoutBinding;
import com.jiamao.goplay.ui.photo.vm.PhotoUploadVM;
import com.jiamao.goplay.ui.photo.vm.PhotoViewItmeVM;
import com.jiamao.goplay.ui.photo.vm.PhotoViewVM;
import com.yuyh.library.imgsel.ISNav;
import com.yuyh.library.imgsel.config.ISListConfig;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

public class PhotoUploadFragment extends BaseMVVMFragment<PhotoUploadLayoutBinding, PhotoUploadVM> {
    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.photo_upload_layout;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }


    @Override
    public PhotoUploadVM initViewModel() {
        //使用自定义的ViewModelFactory来创建ViewModel，如果不重写该方法，则默认会调用LoginViewModel(@NonNull Application application)构造方法
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(PhotoUploadVM.class);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.pSwitchEvent.observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String aBoolean) {

                      showPhoto();
                //  NimUIKit.startP2PSession(getActivity(),aBoolean);

            }
        });
    }

    @Override
    public void initData() {
        super.initData();

        String img =  getArguments().getString("img");
        int  from = getArguments().getInt("from");
        int type = getArguments().getInt("type");
        viewModel.from = from;
        viewModel.type = type;
        ImmersionBar.with(this). keyboardEnable(false).statusBarDarkFont(false).titleBar(binding.getRoot().findViewById(R.id.toolbar)).init();
        viewModel.imgUrl.set(img);


    }


    public void showPhoto(){
        // 自由配置选项
        ISListConfig config = new ISListConfig.Builder()
                // 是否多选, 默认true
                .multiSelect(false)
                // 是否记住上次选中记录, 仅当multiSelect为true的时候配置，默认为true
                .rememberSelected(false)
                // “确定”按钮背景色
                .btnBgColor(Color.GRAY)
                // “确定”按钮文字颜色
                .btnTextColor(Color.BLUE)
                // 使用沉浸式状态栏
                .statusBarColor(Color.BLACK)
                // 返回图标ResId
                .backResId(R.mipmap.icon_back)
                // 标题
                .title("图片")
                // 标题文字颜色
                .titleColor(Color.BLACK)
                // TitleBar背景色
                .titleBgColor(Color.WHITE)
                // 裁剪大小。needCrop为true的时候配置
                .cropSize(1, 1, 200, 200)
                .needCrop(false)
                // 第一个是否显示相机，默认true
                .needCamera(false)
                // 最大选择图片数量，默认9
                .maxNum(1)
                .build();
// 跳转到图片选择器
        ISNav.getInstance().toListActivity(this, config, 1);



    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // 图片选择结果回调
        if (requestCode ==  1  && data != null) {
            List<String> pathList = data.getStringArrayListExtra("result");

            viewModel.imgUrl.set(pathList.get(0));
            viewModel.uploadImage();
            //  viewModel.uploadImage(pathList.get(0));

        }
    }

}
